window.onload = function () {
    fullTable(emList);
}

function fullTable(array) {
    let str = "";
    array.forEach(n => {
        str += `<tr><td>${n.code}</td><td>${n.name}</td><td>${n.money}</td><td>${n.sex}</td>
<td style="display: flex;justify-content:space-evenly"><input type="button" value="删除" onclick="del(${n.code})"><input type="button" value="修改" onclick="findById(${n.code})"></td>
</tr>`;
    });
    $("data").innerHTML = str;
}

function $(id) {
    return document.getElementById(id);
}

function findByName() {
    var nameStr = $("findByName").value;
    var newList = emList.filter(n => n.name.indexOf(nameStr) != -1);
    fullTable(newList);
}

function del(code) {
    //找到指定编号下标
    var index = emList.findIndex(n => n.code == code);
    emList.splice(index, 1);
    fullTable(emList);
}

function showAdd() {
    $("addDiv").style.visibility = "visible";
}

function add() {
    if (emList.find(n=>n.code==$("codeTxt").value)){
        $("err").innerHTML="该ID已经存在";
        return;
    }
    var obj = {
        code: $("codeTxt").value,
        name: $("emNameTxt").value,
        money: $("moneyTxt").value,
        sex: $("sexTxt").value
    };
    emList.push(obj);
    fullTable(emList);
    $("addDiv").style.visibility = "hidden";

}

function findById(code) {
    var obj = emList.find(n => n.code == code);
    $("codeSp").innerHTML = obj.code;
    $("nameSp").innerHTML = obj.name;
    $("updateMoneyTxt").value = obj.money;
    $("sexSp").innerHTML = obj.sex;
    $("updateDiv").style.visibility = "visible";

}

function update() {
    var obj = emList.find(n => n.code == $("codeSp").innerHTML)
    obj.money = parseInt($("updateMoneyTxt").value);
    fullTable(emList);
    $("updateDiv").style.visibility = "hidden";
}